import { payRequestApplet } from '@/API/api.js'
import {RetCodeMsg} from "./errorCode";
import {postPointData} from "./point/postPointData";
export default function(){
	const wxPay = (orderId)=>{
		wx.login({
			success:(res) => {
				let code = res.code
				let params = {
					code,
					orderId,
					payType: 8,
					Authorization: uni.getStorageSync('accessToken')
				}
				payRequestApplet(params).then(res=> {
					postPointData({code:'603089', eventName:'orders_pay'})
					// console.log('res------', res);
					const { code, data } = res;
					if(code=== 2000) {
						if (data.orderInfo) {
							const { timeStamp, nonceStr, signType, sign } = data.orderInfo
							wx.requestPayment({
								timeStamp: String(timeStamp),
								nonceStr,
								package: data.orderInfo.package,
								signType,
								paySign: sign,
								success(res) {
									// console.log('api:success:', res);
								},
								fail(res) {
									// console.log('api:fail:', res);
								},
								complete: (info) => {
									uni.navigateTo({
										url: `/jvss/pageCloud/order/details?orderId=${orderId}`
									});
								}
							})
						} else {
							uni.navigateTo({
								url: `/jvss/pageCloud/order/details?orderId=${orderId}`
							});
						}
					} else {
						uni.showToast({
							title: '支付失败',
							icon: 'none'
						});
					}

				}).catch(() => {
					//联网失败, 结束加载
					uni.showToast({
						title: '支付失败',
						icon: 'none'
					});
				})

			}
		})
	}
	return {
		wxPay
	}
}